﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using GSQ.BusinessEntities;
using GSQ.BusinessLogic;

namespace UIapp.Dialogos
{
    public partial class frmNcf : Plantillas.frmDialogPlantilla, Interfaces.IPantalla
    {
        public frmNcf(NCF entity)

        {
            InitializeComponent();

            try
            {
                if (entity == null)
                {
                    this.newEntity = new NCF();
                    this.oldEntity = null;
                }
                else
                {
                    this.oldEntity = entity;
                    this.newEntity = new NcfBL ().GetById(entity.ID);
                }
                this.BindControls();
            }
            catch (Exception ex)
            {
                Comun.ShowMessage(ex);
            }
        }

        #region Implementacion IPantalla
        public int ComandosSoportados
        {
            get { return 0; }
        }

        public void Nuevo()
        {
            throw new NotImplementedException();
        }

        public void Modificar()
        {
            throw new NotImplementedException();
        }

        public void Eliminar()
        {
            throw new NotImplementedException();
        }

        public void Refrescar()
        {
            throw new NotImplementedException();
        }

        public void Buscar()
        {
            throw new NotImplementedException();
        }

        public void AgregarProducto(Controles.TransferEntity tranferEntity)
        {
            throw new NotImplementedException();
        }
        #endregion  

        #region Variables
        public NCF  oldEntity;
        public NCF  newEntity;
        bool entityError = false;
        #endregion

        #region Metodos auxiliares
        public void BindControls()
        {
            this.lblPrefijo.DataBindings.Add("Text", this.newEntity, "NCF1");
            this.lblDescripcion.DataBindings.Add("Text", this.newEntity, "DESCRIPCION");
            this.lblCondicion.DataBindings.Add("Text", this.newEntity.CONDICIONES , "CONDICION");
            this.lblValue.Text = this.newEntity.VALUE.ToString("N2");
            Binding s = new Binding("Text", this.newEntity, "HASTA", true, DataSourceUpdateMode.OnValidation, null, "N0");
            s.DataSourceNullValue = 1;
            this.txtHasta.DataBindings.Add(s);
        }

        private void Guardar()
        {
            if (newEntity.IsValid())
            {
                if (this.oldEntity == null)
                {
                    this.newEntity.ACTIVO = true;
                    new NcfBL ().Insert(this.newEntity);
                }
                else
                {                   
                    new NcfBL().Update(this.newEntity, this.oldEntity);
                    this.oldEntity = this.newEntity;
                }
                this.entityError = false;
            }
            else
            {
                this.entityError = true;
                Comun.ShowMessage(newEntity);
            }
        }
        #endregion

        #region Tratamiento de eventos

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.newEntity.VALUE > this.newEntity.HASTA)
                {
                    Comun.ShowInMainMessage("Valor Máximo no puede ser menor que el Valor Actural.");
                    return;
                }
                this.backgroundWorker.RunWorkerAsync();
                Comun.ShowLoadingFRM();
            }
            catch (Exception ex)
            {
                Comun.ShowMessage(ex);
            }
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Guardar();
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Comun.CloseLoadingFRM();
            if (e.Error != null)
                Comun.ShowMessage(e.Error);
            else if (this.entityError)
                return;
            else
            {
                Comun.ShowInMainMessage("Datos guardados correctamente...");
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        #endregion
    }
}
